package ai.moran.test.listener.confirmListener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

public class Customer {

    public static void main(String[] args) throws Exception {

        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("10.80.20.209");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("abm-order");
        connectionFactory.setUsername("dc_shop");
        connectionFactory.setPassword("JF8tga3#s3");

        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        //4 声明交换机
        String exchangeName = "test_confirm_exchange";
        String exchangeType = "direct";
        channel.exchangeDeclare(exchangeName, exchangeType, false, false, false, null);

        //5 声明（创建）一个队列
        String queueName = "test_confirm_queue";
        // 1.队列名 2.是否持久化 3.是否独占（顺序消费）4.自动删除（如果和交换机没有绑定关系）5.扩展参数
        channel.queueDeclare(queueName, true, false, false, null);

        //6 绑定交换机
        String routingKey = "test.confirm";
        channel.queueBind(queueName, exchangeName, routingKey);

        //7 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

        //8 设置Channel
        channel.basicConsume(queueName, true, queueingConsumer);

        while (true) {
            //9 获取消息
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.err.println("消费端: " + msg);
        }

    }
}
